/*! AdBlock for Chromium's foreground script. */
//exclude: "*://*.edu.cn/*"

(function(){
	'use strict';

	// Donot run in XMLDocuments.
	if(document instanceof HTMLDocument === false)
		return;

	// Create and insert an HTMLStyleElement.
	var ncss = document.createElement('style');
	ncss.setAttribute('data-ext', '__AdBlock__');
	document.documentElement.appendChild(ncss);

	// Prepare the msg which will be sent to backgroundPage.
	var _hostname = location.hostname.replace(/\.sixxs\.org$/, ''),
		_href = _hostname + location.pathname + location.search;
	var	msg = {
			'hostname' : _hostname,
			'href' : _href,
			'cached' : (typeof sessionStorage['__AdBlock__'] !== 'undefined')
	};

	// If specific selectors cached, insert them immediately.
	if(msg.cached)
		ncss.sheet.addRule(sessionStorage['__AdBlock__'], 'display:none!important');


	// Send msg to backgroundPage.
	// Always get an array of selectors back, with specific selectors at array's end.
	chrome.extension.sendRequest( msg, function(r) {
		'use strict';

		// Remove the HTMLStyleElement if this href is whitelisted.
		if(r[0] === '#__adblock_null_selector__') {
			document.documentElement.removeChild(ncss);
			return;
		}

		for(var i = r.length; i--;)
			ncss.sheet.addRule(r[i], 'display:none!important');

		// Cache specific selectors in sessionStorage.
		if(!msg.cached && navigator.cookieEnabled)
			sessionStorage['__AdBlock__'] = r.pop();
	} );

})();